//
//  BGView.m
//  点击触摸
//
//  Created by 伍银荣 on 2021/7/5.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "BGView.h"
#import "CustomView.h"

@interface BGView ()
@property (strong, nonatomic) IBOutlet CustomView *customView;

@end

@implementation BGView

//在手指开始触摸的时候调用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__func__);
    //获取触摸对象
    UITouch *t = touches.anyObject;
    //获取当前点的位置
    CGPoint currentPoint = [t locationInView:self];
    //设置CustomView的center等于当前点的位置
    self.customView.center = currentPoint;
}


//手指在View移动的时候调用
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //获取触摸对象
    UITouch *t = touches.anyObject;
    //获取当前点的位置
    CGPoint currentPoint = [t locationInView:self];
    //获取上一点的位置
    CGPoint lastPoint = [t previousLocationInView:self];
    //获取两点间x的偏移量
    CGFloat offsetX = currentPoint.x - lastPoint.x;
    //获取两点间y的偏移量
    CGFloat offsetY = currentPoint.y - lastPoint.y;
    //设置CustomView的center等于当前view的center加上偏移量
    self.customView.center = CGPointMake(self.customView.center.x + offsetX, self.customView.center.y + offsetY);
    
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
